const classModel = require('../model/classModel');

module.exports.findAll = async function(){
    return await classModel.find();
}

module.exports.find = async function(page,searchParams){
    let {current,pageSize} = page;
    console.log(current,pageSize)
    // 根据查询条件获取总数
    let total = await classModel.countDocuments(searchParams);
    // 根据查询条件获取具体数量的数据
    let data = await classModel.find(searchParams)
                    .sort({index:1})
                    .limit(pageSize)
                    .skip((current - 1) * pageSize);
    return {
        current,pageSize,total,rows:data,
    }
    
}
module.exports.findById = async function(id){
    return await classModel.findById(id);
}
module.exports.updateIndex = async function(beforeIndex,currentIndex,incOrDec){
    if(incOrDec > 0){
        return await classModel.updateMany({index:{$gte:currentIndex,$lt:beforeIndex}},{$inc:{index:incOrDec}});
    }else{
        return await classModel.updateMany({index:{$gt:beforeIndex,$lte:currentIndex}},{$inc:{index:incOrDec}});
    }
}
module.exports.update = async function(cls){
    return await classModel.updateOne({_id:cls._id},cls);
}